﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Wild2.Models.Favorites
{
    public class Favorites
    {
        public static void AddFavoriteToSession(string Name, string Url)
        {
            HttpContext.Current.Session[Models.Constants.SessionConstants.FAVORITES] = Name + "|" + Url;
        }

        public static void AddFavoriteToSession(string Name)
        {
            HttpContext.Current.Session[Models.Constants.SessionConstants.FAVORITES] = Name + "|" + HttpContext.Current.Request.Path;
        }

        public static List<FavoriteData> GetFavorites(int userID)
        {
            List<FavoriteData> favorites = new List<FavoriteData>();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"SELECT [ID], [Name], [Date], [ID_User], [Address] FROM [Favorites] WHERE ([ID_User] = @UID) ORDER BY [Date] DESC", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int).Value = userID;
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    FavoriteData favorite = new FavoriteData();
                    favorite.ID = Convert.ToInt32(reader["ID"]);
                    favorite.Name = reader["Name"].ToString();
                    favorite.Url = reader["Address"].ToString();
                    favorite.UserID = userID;
                    favorite.Date = Convert.ToDateTime(reader["Date"]);
                    favorites.Add(favorite);
                }
                reader.Close();
                cmd.Connection.Close();
            }
            return favorites;
        }

        public static void AddFavorite(int userID, string name, string url)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"INSERT INTO [Favorites]([Name], [ID_User], [Address]) VALUES(@Name, @UID, @Addr)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int).Value = userID;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = name;
                cmd.Parameters.Add("@Addr", SqlDbType.NVarChar, 500).Value = url;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }

        public static void DeleteFavorite(int userID, int favoriteID)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(@"DELETE FROM [Favorites] WHERE (ID_User = @UID) AND (ID = @FID)", conn);
                cmd.Parameters.Add("@UID", SqlDbType.Int).Value = userID;
                cmd.Parameters.Add("@FID", SqlDbType.Int).Value = favoriteID;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }
    }
}